/**  
* 
* <p>Copyright: Copyright (c) 2017</p>  
* <p>Company: http://www.richfit.com/</p>  
* @author hushouquan
* @date 2020年5月25日  
* @version 1.0  
*/  
package com.rf.richfitwheel.admin.sys.controller;

import java.util.HashMap;
import java.util.Map;

import com.rf.richfitwheel.admin.sys.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rf.richfitwheel.admin.annotation.ModifyAnnotaionFactory;
import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.Config;
import com.rf.richfitwheel.admin.sys.model.User;
import com.rf.richfitwheel.admin.sys.service.ConfigService;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.contants.CommonServiceError;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;

/**  
*  
* @author hushouquan
* @date 2020年5月25日  
*/
@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL+"/config")
public class ConfigController {
	private static Logger log = LoggerFactory.getLogger(ConfigController.class);
	
	@Autowired
	private ConfigService configService;
	
	/**
	 * 分页查询系统参数
	 * 
	 * @param page
	 * @param limit
	 * @param dataForm
	 * @return
	 */
	@GetMapping("/list")
	public R list(String page, String limit, String dataForm){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);
		PageUtils pages = configService.queryPage(params);
		return R.ok().put("page", pages);
	}
	
	/**
	 * 新增系统参数
	 * 
	 * @param config
	 * @return
	 */
	@UserOperateLog(module="系统功能",methods="save",description="新增系统参数",functionNo="101052")
	@PostMapping("/save")
	public R save(@RequestBody Config config) {
		try {
			UserVO user = UserUtil.getUser();
			configService.save(config, user);
			//给日志注解传入业务id		
	        ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "save", "bussid", config.toString(), config.getClass());
			return R.ok();
		} catch(BusinessException e) {
			return R.error(e.getMessage());
		}catch (Exception e) {
			log.error(CommonServiceError.SERVICE_SAVE + "系统参数" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
			return R.error(CommonServiceError.SERVICE_SAVE + "系统参数" + CommonServiceError.SERVICE_FAILURE.getMsg());
		}
	}
	
	/**
	 * 修改系统参数
	 * 
	 * @param config
	 * @return
	 */
	@UserOperateLog(module="系统功能",methods="save",description="修改系统参数",functionNo="101053")
	@PostMapping("/update")
	public R update(@RequestBody Config config) {
		try {
			UserVO userVO = UserUtil.getUser();
			configService.update(config, userVO);
			//给日志注解传入业务id		
	        ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "update", "bussid", config.getId(), config.getClass());
			return R.ok();
		} catch (Exception e) {
			log.error(CommonServiceError.SERVICE_SAVE + "系统参数" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
			return R.error(CommonServiceError.SERVICE_SAVE + "系统参数" + CommonServiceError.SERVICE_FAILURE.getMsg());
		}
	}
	
	/**
	 * 删除配置
	 */
	@UserOperateLog(module="系统功能",methods="delete",description="删除系统参数",functionNo="101054")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids){
		try {
			UserVO user = UserUtil.getUser();
			configService.deleteBatch(ids);
			//给日志注解传入业务id		
	        ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "update", "bussid", ids.toString(), ids.getClass());
			return R.ok();
		} catch (Exception e) {
			log.error(CommonServiceError.SERVICE_DELETE + "系统参数" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
			return R.error(CommonServiceError.SERVICE_DELETE + "系统参数" + CommonServiceError.SERVICE_FAILURE.getMsg());
		}
	}
	
	/**
	 * 根据id查询系统参数
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) {
		Config config = configService.selectById(id);
		return R.ok().put("config", config);
	}
	
	/**
	 * 根据参数key获取参数值，给外部调用，没有找到返回null
	 * 
	 * @param paramKey
	 * @return
	 */
	@GetMapping("/getParamVlueByKey/{paramKey}")
	public String getParamVlueByKey(@PathVariable("paramKey") String paramKey) {
		Config config = configService.getParamVlueByKey(paramKey);
		return config == null ? null : config.getParamValue();
	}
	
	/**
	 * 检查参数编码是否存在
	 * 
	 * @param paramKey
	 * @return
	 */
	@GetMapping("/checkHasParamkey/{paramKey}")
	public R checkHasParamkey(@PathVariable("paramKey") String paramKey) {
		boolean ck = configService.checkHasParamkey(paramKey);
		if(ck) {
			return R.error("参数编码已被占用");
		}
		return R.ok();
	}
}
